package com.android.pagemarket.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.pagemarket.R;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-14
 * @see : TODO
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class IndexTabView extends LinearLayout
{
    private TextView  tab_tx;
    private ImageView tab_indac;
    private TextView  ic_unread;
    private View      tab_indac_lay;
    private int       resIds[];
    
    public IndexTabView(Context paramContext, AttributeSet paramAttributeSet)
    {
      super(paramContext, paramAttributeSet);
      View row = inflate(getContext(), R.layout.view_index_tab, null);
      addView(row,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
      tab_tx = (TextView)findViewById(R.id.tab_tx);
      tab_indac = (ImageView)findViewById(R.id.tab_indac);
      ic_unread = (TextView)findViewById(R.id.ic_unread);
      tab_indac_lay = findViewById(R.id.tab_indac_lay);
      setGravity(Gravity.CENTER);
      resIds = new int[6];
    }
    
    public void setBackgroundResource(int id1, int id2)
    {
        resIds[0]  = id1;
        resIds[1]  = id2;
        
        setBackgroundResource(resIds[0]);
    }
    
    public void setImageResource(int id1, int id2)
    {
        resIds[2]  = id1;
        resIds[3]  = id2;
        
        tab_indac.setImageResource(resIds[2]);
    }
    
    public void setText(int text, int id1, int id2)
    {
        tab_tx.setText(text);        
        resIds[4]  = id1;
        resIds[5]  = id2;       
        tab_tx.setTextColor(resIds[4]);
    }
    
    public void setUnreadNum(int num)
    {
        Log.d("main", "-->setUnreadNum " + num);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)tab_indac_lay.getLayoutParams();
        if(num > 0)
        {
            ic_unread.setText(num+"");
            ic_unread.setVisibility(View.VISIBLE);
            lp.rightMargin = 4;
            lp.leftMargin  = 4;
            tab_indac_lay.setLayoutParams(lp);
        }else
        {
            ic_unread.setVisibility(View.GONE);
            lp.rightMargin = 8;
            lp.leftMargin  = 8;
            tab_indac_lay.setLayoutParams(lp);
        }
        
        tab_indac_lay.requestLayout();
    }
    
    public void setClick()
    {
        tab_indac.setVisibility(View.VISIBLE);
    }
    
    @Override
    public void setSelected(boolean selected)
    {
        super.setSelected(selected);
        setBackgroundResource(selected ? resIds[1]: resIds[0]);
        tab_indac.setImageResource(selected ? resIds[3]: resIds[2]);
        tab_tx.setTextColor(selected ? resIds[5]: resIds[4]);
    }
}
